package yxy.game.pm2.bean.module.rank.openserviceactive.boosrushrank;

import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.redisson.api.RBucket;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.module.rank.openserviceactive.OpenServiceRushRank;

import java.util.Collection;

/**
 * 开服冲榜-联盟boos实现类
 *
 * @ClassName OpenServiceBoosRushRank
 * @Author jingtao-zheng
 * @Date 2022/7/13 18:55
 * @Version 1.0
 **/
public class OpenServiceBoosRushRank extends OpenServiceRushRank {


    public OpenServiceBoosRushRank(int areaId, int type) {
        super(areaId, type);
    }

    /**
     * 更新联盟boos排行榜
     */
    static public boolean updateBoosRanking(String playerCode, int score, String unioncode, int rankingType) {
        AreaBean areaBean = AreaModule.area(playerCode);
        int areaId = areaBean.getAreaId();
        OpenServiceBoosRushRank osrk = new OpenServiceBoosRushRank(areaId, rankingType);
        //计算活动是否开启
        boolean extracted = extracted(playerCode, rankingType);
        if (!extracted) {
            return false;
        }
        osrk.addAndSet(unioncode, score);
        return true;
    }

    /**
     * 删除联盟boos排行榜
     * 防止有人创建后,又解散联盟,导致数据错误
     */
    static public boolean deleteBoosRanking(String playerCode,String unioncode, int rankingType) {
        AreaBean areaBean = AreaModule.area(playerCode);
        int areaId = areaBean.getAreaId();
        OpenServiceBoosRushRank osrk = new OpenServiceBoosRushRank(areaId, rankingType);
        //计算活动是否开启
        boolean extracted = extracted(playerCode, rankingType);
        if (!extracted) {
            return false;
        }
        osrk.remove(unioncode);
        return true;
    }

    static public boolean isStartActivity(String playerCode, int rankingType) {
        return extracted(playerCode, rankingType);
    }

    private static boolean extracted(String playerCode, int rankingType) {
        RedisConnection connection = RedisManager.getInstance().getConnection(RedisType.REDIS_DATA);
        JSONObject json = null;
        try {
            /**
             * 由于拿不到活动控制类
             * 暂时只能通过key拿出json来用
             */
            AreaBean areaBean = AreaModule.area(playerCode);
            int areaId = areaBean.getAreaId();
            RBucket<Object> bucket = RedisManager.client(connection)
                    .getBucket(String.format("AREA:%s:ACTTYPE:%s", areaId, rankingType));
            Object o = bucket.get();
            json = (JSONObject) JSONObject.toJSON(o);
            //防止其他区没开活动而报错
            if(null == json){
                return false;
            }
        } catch (RedisException e) {
            e.printStackTrace();
        }
        //获取当前时间
        DateTime now = DateTime.now();
        /**
         *  获取活动开始时间
         */
        //获取策划配置的开始时间和结束时间
        DateTime chStartTime = DateTime.parse((String) ((JSONObject) json.get("timeline")).get("startTime"));
        DateTime chendTime = DateTime.parse((String) ((JSONObject) json.get("timeline")).get("endTime"));
        //获取开服时间
        PlayerBean playerBean = PlayerBean.get(playerCode);
        ServerBean serverBean = ServerBean.get(playerBean.getServerId());
        DateTime starttime = serverBean.getStarttime();

        String s = starttime.toString("yyyy-MM-dd");
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
        starttime = DateTime.parse(s, format);

        //活动开启条件
        int daycondition = (int) json.get("daycondition");
        //计算真实活动开始时间=开服时间 + 活动开启条件表示的天数
        DateTime realStartDateTime = starttime.plusDays(daycondition);
        /**
         * 获取活动结束时间
         */
        //活动运行天数
        int operationcycle = (int) json.get("operationcycle");
        ;
        // 真实活动结束时间 = 真实活动开始时间 + 活动运行天数
        DateTime realEndDateTime = realStartDateTime.plusDays(operationcycle);

        //计算是否开启活动
        return chStartTime.getMillis() <= now.getMillis()
                &&
                now.getMillis() <= chendTime.getMillis()
                &&
                realStartDateTime.getMillis() <= now.getMillis()
                &&
                now.getMillis() <= realEndDateTime.getMillis()
                ;

    }


//    /**
//     * 是否更新排行榜
//     */
//    private int isUpdateBoosRanking(String playerCode, int score) {
//        RankEntry rankEntry = get(playerCode, OpenServiceRushRankEnum.开服冲榜_齐心协力.getType());
//        long value = 0;
//        if(null != rankEntry){
//            value = rankEntry.getValue();
//        }
//        return score - (int)value;
//    }

    /**
     * 获取排名信息
     */
    static public Collection<RankEntry> top(String playercode, int rankrushtype, int size) {
        AreaBean areaBean = AreaModule.area(playercode);
        int areaId = areaBean.getAreaId();
        OpenServiceBoosRushRank cbrr = new OpenServiceBoosRushRank(areaId, rankrushtype);
        return cbrr.top(size);
    }

    /**
     * 获取一定范围的排名信息
     */
    static public Collection<RankEntry> range(int areaid, int rankrushtype, int min, int max) {
        OpenServiceBoosRushRank cbrr = new OpenServiceBoosRushRank(areaid, rankrushtype);
        return cbrr.range(min, max);
    }

    /**
     * 获取单人信息
     */
    static public RankEntry get(String playercode, String unionCode, int rankrushtype) {
        AreaBean areaBean = AreaModule.area(playercode);
        int areaId = areaBean.getAreaId();
        OpenServiceBoosRushRank cbrr = new OpenServiceBoosRushRank(areaId, rankrushtype);
        RankEntry rankEntry = cbrr.get(unionCode);
        return rankEntry;
    }

    /**
     * 获取单人排名
     */
    static public int rank(String playercode, String unionCode, int rankrushtype) {
        AreaBean areaBean = AreaModule.area(playercode);
        int areaId = areaBean.getAreaId();
        OpenServiceBoosRushRank cbrr = new OpenServiceBoosRushRank(areaId, rankrushtype);
        int rank = cbrr.rank(unionCode);
        return rank;
    }
}